#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include "touch.h"
#include "bmp.h"
#include "lcd.h"
#include "word.h"
#include "common.h"

// int signals=-1;

// 延时函数
void delay_ms(int time)
{
    usleep(time * 1000);
}

//蜂鸣器和led灯控制
int led_beep_ctrl(char *filename, int state)
{
    int fd = open(filename, O_RDWR);
    if (fd == -1)
    {
        perror("open led error:");
        return -1;
    }
    write(fd, &state, 4);
    close(fd);
}

//歌曲控制
int music_ctrl(char *musicname)
{
    system(musicname);
}


// 播放视频文件
void play_video(const char *filename) {
    char command[256];
    snprintf(command, sizeof(command), "mplayer -zoom -x 800 -y 480 %s", filename);
    system(command);
}

//后台播放视频文件
void play_video_backstage(const char *filename) {
    char command[256];
    snprintf(command, sizeof(command), "mplayer -zoom -x 800 -y 480 %s > /dev/null 2>&1 &", filename);
    system(command);
}


// 暂停视频播放
void pause_video(void) {
    system("killall -STOP mplayer");
}

// 继续播放视频
void continue_video(void) {
    system("killall -CONT mplayer");
}

// 停止视频播放
void stop_video(void) {
    system("pkill -9 mplayer");
}

//滑动特效
int show_bmp_with_blinds(int x0, int y0, char *filename, int strip_width, int delay_us, int direction)
{
    int fd = open(filename, O_RDWR);
    if (fd == -1)
    {
        perror("OPEN ERROR");
        return -1;
    }

    int width = 0, height = 0;
    int depth = 0;
    if (lseek(fd, 0x12, SEEK_SET) == -1 || read(fd, &width, 4) != 4 || lseek(fd, 0x16, SEEK_SET) == -1 || read(fd, &height, 4) != 4 || lseek(fd, 0x1c, SEEK_SET) == -1 || read(fd, &depth, 4) != 4)
    {
        perror("Read ERROR");
        close(fd);
        return -1;
    }

    int laizi = 0;     // 表示要填充的字节数
    int line_size = 0; // 实际的字节数
    laizi = 4 - (abs(width) * (depth / 8)) % 4;
    if (laizi == 4)
    {
        laizi = 0;
    }

    line_size = abs(width) * (depth / 8) + laizi;
    unsigned char buf[abs(height) * line_size];
    if (lseek(fd, 0x36, SEEK_SET) == -1 || read(fd, buf, abs(height) * line_size) != abs(height) * line_size)
    {
        perror(" Read(1) ERROR");
        close(fd);
        return -1;
    }

    unsigned b, g, r, a = 0;
    int i, j, k;
    int num;
    int color;

    int num_segments = (abs(width) + strip_width - 1) / strip_width; // 计算条块数量

    // 开始从左到右或从右到左同时显示8块
    for (i = 0; i < abs(width); i++) // 控制水平显示，从左到右/右到左移动
    {
        for (k = 0; k < num_segments; k++) // 对每块同时显示
        {
            for (j = 0; j < abs(height); j++) // 每块中的每行
            {
                if (direction == RIGHT) // 从左到右
                {
                    if (k * strip_width <= i && i < (k + 1) * strip_width) // 仅在当前条块内显示
                    {
                        num = j * line_size + i * (depth / 8);
                        b = buf[num++];
                        g = buf[num++];
                        r = buf[num++];
                        if (depth == 32)
                        {
                            a = buf[num++];
                        }
                        color = (a << 24) | (r << 16) | (g << 8) | b;

                        // 绘制该像素
                        display_point(x0 + i, y0 + (height > 0 ? abs(height) - 1 - j : j), color);
                    }
                }
                else if (direction == LEFT) // 从右到左
                {
                    if (k * strip_width <= (abs(width) - 1 - i) && (abs(width) - 1 - i) < (k + 1) * strip_width) // 仅在当前条块内显示
                    {
                        num = j * line_size + (abs(width) - 1 - i) * (depth / 8);
                        b = buf[num++];
                        g = buf[num++];
                        r = buf[num++];
                        if (depth == 32)
                        {
                            a = buf[num++];
                        }
                        color = (a << 24) | (r << 16) | (g << 8) | b;

                        // 绘制该像素
                        display_point(x0 + (abs(width) - 1 - i), y0 + (height > 0 ? abs(height) - 1 - j : j), color);
                    }
                }
            }
        }

        usleep(delay_us); // 控制移动速度，形成滑动效果
    }

    // 关闭文件
    close(fd);
    return 0;
}


